package com.yzt.zhuju.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.ProgressDialog;
import com.yzt.p_base.AppUtils;
import com.yzt.p_base.utils.LogDataUtils;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   :
 */
public abstract class BaseDialog extends DialogFragment implements View.OnClickListener {

    public FragmentActivity mActivity;
    public Handler mHandler = new Handler();
    private ProgressDialog mProgressDialog;
    protected View rootView;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (FragmentActivity) context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DialogCustomeStyle);
        if (mActivity == null) {
            mActivity = getActivity();
        }
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutRes(), container, false);
        bindView(rootView);
        return rootView;
    }

    public final <E extends View> E findViewById(int id) {
        try {
            return rootView.findViewById(id);
        } catch (ClassCastException ex) {
            LogDataUtils.e("Could not cast View to concrete class.", ex);
            throw ex;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        Window window = dialog.getWindow();
        if (window != null) {
            // 设置宽度为屏宽, 靠近屏幕底部。
            // 一定要设置Background，如果不设置，window属性设置无效
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = getGravity();
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }
        dialog.setCanceledOnTouchOutside(true);
    }

    /**
     * @return Gravity.BOTTOM
     */
    public abstract int getGravity();

    /**
     * 获取布局资源文件
     *
     * @return 布局资源文件id值
     */
    @LayoutRes
    public abstract int getLayoutRes();

    /**
     * 绑定
     *
     * @param v view
     */
    public abstract void bindView(View v);

    /**
     * 在onStart方法中调用
     */
    // 设置dialog透明
    protected void setTransParentDialog() {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.dimAmount = 0;
            lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
            window.setAttributes(lp);
        }
    }

    public static BaseDialog showDialog(FragmentActivity activity, Class<? extends BaseDialog> dialogClass) {
        return showDialog(activity, dialogClass, null);
    }

    public static BaseDialog showDialog(FragmentActivity activity, Class<? extends BaseDialog> dialogClass, Bundle args) {
        if (activity == null) {
            return null;
        }
        BaseDialog dialogFragment = null;
        try {
            dialogFragment = dialogClass.newInstance();
            if (args != null) {
                dialogFragment.setArguments(args);
            }
            dialogFragment.mActivity = activity;
            FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
            transaction.add(dialogFragment, dialogClass.getName());
            transaction.commitAllowingStateLoss();
        } catch (IllegalAccessException | java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return dialogFragment;
    }

    /**
     * 只给礼物用
     */
    public static BaseDialog showGiftDialog(FragmentActivity activity, BaseDialog dialogFragment) {
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        transaction.add(dialogFragment, "UBiDialogFragment");
        transaction.commitAllowingStateLoss();
        return dialogFragment;
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 显示进度
     */
    public void showMProgress() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mActivity);
        }
        if (!mActivity.isFinishing()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (!mActivity.isDestroyed()) {
                    mProgressDialog.show();
                }
            } else {
                mProgressDialog.show();
            }
        }
    }

    /**
     * 显示进度
     */
    public void showMProgress(String msg) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mActivity);
        }

        if (!mActivity.isFinishing()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (!mActivity.isDestroyed()) {
                    mProgressDialog.setShowContent(msg);
                    mProgressDialog.show();
                }
            } else {
                mProgressDialog.setShowContent(msg);
                mProgressDialog.show();
            }
        }
    }

    public boolean isShowing() {
        if (getDialog() == null) {
            return false;
        } else {
            return getDialog().isShowing();
        }
    }

    /**
     * 关闭progress提示框
     */
    public void closeProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.cancel();
        }
    }

    public View getErrorView(String text) {
        return getEmptyView(R.mipmap.icon_network_error, text);
    }

    public View getEmptyView() {
        return getEmptyView(R.mipmap.icon_empty_data, getString(R.string.page_empty));
    }

    public View getEmptyView(String text) {
        return getEmptyView(R.mipmap.icon_empty_data, text);
    }

    public View getEmptyView(int imageId, String text) {
        if (mActivity == null) {
            return new View(AppUtils.getContext());
        }
        View view = LayoutInflater.from(mActivity).inflate(R.layout.layout_empty_view, null);
        ImageView emptyImage = view.findViewById(R.id.iamge_empty);
        TextView emptyText = view.findViewById(R.id.text_empty);
        emptyImage.setImageResource(imageId);
        emptyText.setText(text);
        return view;
    }
}